package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.dao.AppGeneralfeeschemeMapper;
import com.eco.fanliapp.entity.AppGeneralFeeScheme;
import com.eco.fanliapp.service.IAppGeneralfeeschemeService;
import com.eco.fanliapp.service.RedisTemplateService;
import com.eco.fanliapp.utils.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 普通佣金分佣方案 服务层实现
 * 
 * @author xiaoxx
 * @date 2019-05-12
 */
@Service
public class AppGeneralfeeschemeServiceImpl extends ServiceImpl<AppGeneralfeeschemeMapper, AppGeneralFeeScheme> implements IAppGeneralfeeschemeService
{
	@Autowired
	private AppGeneralfeeschemeMapper appGeneralfeeschemeMapper;

	@Autowired
	private RedisTemplateService redisTemplateService;

	/**
	 * 查询普通佣金分佣方案信息
	 *
	 * @param generalFeeSchemeId 普通佣金分佣方案ID
	 * @return 普通佣金分佣方案信息
	 */
	@Override
	public AppGeneralFeeScheme selectAppGeneralfeeschemeById(Integer generalFeeSchemeId, int type) {

		AppGeneralFeeScheme appGeneralFeeScheme = null;
		String redisKeyStr = "app_generalFeeScheme_" + generalFeeSchemeId;
		if (type == 1 && redisTemplateService.hasKey(redisKeyStr)) {
			appGeneralFeeScheme = redisTemplateService.getTByKey(redisKeyStr, AppGeneralFeeScheme.class);
		}
		if (appGeneralFeeScheme == null) {
			appGeneralFeeScheme = appGeneralfeeschemeMapper.selectAppGeneralfeeschemeById(generalFeeSchemeId);
			if (appGeneralFeeScheme != null) {
				redisTemplateService.set(redisKeyStr, appGeneralFeeScheme, 60 * 5);
			}
		}
		return appGeneralfeeschemeMapper.selectAppGeneralfeeschemeById(generalFeeSchemeId);
	}
	
	/**
     * 查询普通佣金分佣方案列表
     * 
     * @param appGeneralfeescheme 普通佣金分佣方案信息
     * @return 普通佣金分佣方案集合
     */
	@Override
	public List<AppGeneralFeeScheme> selectAppGeneralfeeschemeList(AppGeneralFeeScheme appGeneralfeescheme)
	{
	    return appGeneralfeeschemeMapper.selectAppGeneralfeeschemeList(appGeneralfeescheme);
	}
	
    /**
     * 新增普通佣金分佣方案
     * 
     * @param appGeneralfeescheme 普通佣金分佣方案信息
     * @return 结果
     */
	@Override
	public int insertAppGeneralfeescheme(AppGeneralFeeScheme appGeneralfeescheme)
	{
		Integer id = appGeneralfeescheme.getGeneralFeeSchemeId();
		if(id != null){
			appGeneralfeescheme.setUpdateTime(System.currentTimeMillis());
			return appGeneralfeeschemeMapper.updateAppGeneralfeescheme(appGeneralfeescheme);
		}else {
			appGeneralfeescheme.setCreateTime(System.currentTimeMillis());
			appGeneralfeescheme.setUpdateTime(System.currentTimeMillis());
			return appGeneralfeeschemeMapper.insertAppGeneralfeescheme(appGeneralfeescheme);
		}


	}
	
	/**
     * 修改普通佣金分佣方案
     * 
     * @param appGeneralfeescheme 普通佣金分佣方案信息
     * @return 结果
     */
	@Override
	public int updateAppGeneralfeescheme(AppGeneralFeeScheme appGeneralfeescheme)
	{
		appGeneralfeescheme.setUpdateTime(System.currentTimeMillis());
	    return appGeneralfeeschemeMapper.updateAppGeneralfeescheme(appGeneralfeescheme);
	}

	/**
     * 删除普通佣金分佣方案对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteAppGeneralfeeschemeByIds(String ids)
	{
		return appGeneralfeeschemeMapper.deleteAppGeneralfeeschemeByIds(Convert.toStrArray(ids));
	}

	@Override
	public int delete(int id) {
		return appGeneralfeeschemeMapper.delete(id);
	}

	@Override
	public AppGeneralFeeScheme selectByCompayId(Integer companyId) {
//		Integer generalFeeSchemeId = 13;
//		AppGeneralFeeScheme appGeneralFeeScheme = appGeneralfeeschemeMapper.selectAppGeneralfeeschemeById(generalFeeSchemeId);
		AppGeneralFeeScheme appGeneralFeeScheme = appGeneralfeeschemeMapper.selectByCompayId(companyId);
		return appGeneralFeeScheme;
	}

}
